package com.course.cdspringboot.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.StatementType;


import java.util.Map;

@Mapper
public interface UserMapper {

    @Select("{call register_user(#{p_username}, #{p_password}, #{role_id}, #{error_code,mode=OUT,jdbcType=INTEGER}, #{error_msg,mode=OUT,jdbcType=VARCHAR})}")
    @Options(statementType = StatementType.CALLABLE)
    void registerUser(Map<String, Object> paramMap);

    @Select("SELECT * FROM users WHERE username = #{username} AND password = #{password}")
    Map<String, Object> findByUsernameAndPassword(String username, String password);

    @Select("SELECT * FROM users WHERE username = #{username}")
    Map<String, Object> findByUsername(String username);

    @Select("SELECT user_roles.rid, roles.name FROM users " +
            "JOIN user_roles on user_roles.uid = users.uid " +
            "JOIN roles on user_roles.rid = roles.rid " +
            "WHERE users.uid = #{userId}")
    Map<String, Object> findRoleByUserId(Long userId);


}
